'''
Created on 2009-10-16

@author: kanghui
'''
import time
def tail(f):
        #f.seek(0,2)
        print f.readline()
        while True:
                line = f.readline()
                print 1,line
                if not line:
                        time.sleep(2)
                        continue
                yield line
                
def grep(lines,searchtext):
    for line in lines:
        if searchtext in line: yield line
        
#event = tail(open("D:/mytools/Apache2.2/logs/access.log","r"));
#lines = grep(event,"event_crm");    
#for line in lines:
#    print 2,line;    


def print_matches(matchtext):
    print "begin match"
    while True:
        line = (yield)
        if matchtext in line:
            print line

def test_match():
    matchers = [
                print_matches("event"),
                print_matches("inv"),
                print_matches("msg")
    ]
    for match in matchers : match.next()
    wwwlogs = tail(open("D:/mytools/Apache2.2/logs/access.log","r"))
    for line in wwwlogs:
        for m in matchers:
            m.send(line)
test_match()
